(function() {
    if (typeof app_show != 'undefined') return;
    var app_show = {
        FN:function() {
        }
    };
    app_show.coms = {};
    app_show.global = this;
    app_show.id = 1000;

    app_show.getId = function(pre) {
        if (typeof pre == 'undefined')
            pre = 'com';
        return 'app_show_' + pre + '_' + app_show.id++;
    };
    app_show.prototype = {};

    app_show.apply = function(o, a) {
        for (var i in a) {
            if (i == 'define')continue;
            o[i] = a[i];
        }
    };
    app_show.create = function(cn, config) {
        var o = eval('new ' + cn + ';');

        if (typeof config == 'undefined') {
            if (typeof o.init != 'undefined')
                o.init();
        } else {
            app_show.apply(o, config);
            if (typeof config.define == 'undefined') {
                if (typeof o.init != 'undefined')
                    o.init();
            }
        }
        app_show.coms[o.id] = o;
        return o;
    };

    app_show.define = function(cn, config) {
        var newClass = function() {
        };
        if (typeof config.extend != 'undefined') {
            var from = app_show.create(config.extend, {define:1});
            newClass.prototype = from;
            newClass.superclass = app_show.create(config.extend, {define:1});
        } else {
            newClass.prototype = {};
        }

        config.className = cn;
        app_show.apply(newClass.prototype, config);
        var a = cn.split('.');
        var o = 'window';
        for (var i = 0 ,l = a.length; i < l; i++) {
            if (typeof o == 'undefined') {
                o = a[i];
            } else {
                o += '.' + a[i];
            }

            var c = eval("typeof " + o);
            if (c == 'undefined') {
                eval(o + " = function(){};");
            }
        }

        var nc = newClass;
        eval(o + ' = nc;');
    };

    window.app_show = app_show;
})();

$(function() {
    app_show.define('app_show.html', {
        renderTo:'body',
        rendered:false,
        render:function(selector) {
            this.selector = selector;
            $(selector).append(this.el);
        },
        destory:function() {
            $(this.selector).remove();
        },
        tpl:[
            '<div>',
            'html = {name}',
            '</div>'
        ],
        data:{
            name:'kevin'
        },
        init:function() {
            this.makeEl();
            this.render(this.renderTo);
        },
        makeEl:function() {
            var s = this.tpl.join('');
            var me = this;
            s = s.replace(/\{(.*?)\}/g, function($0, $1) {
                if (typeof me.data[$1] == 'function') {
                    return me.data[$1]();
                } else {
                    return me.data[$1];
                }
            });
            this.el = $(s);
            if (typeof this.id == 'undefined') {
                this.id = app_show.getId();
            }
            if (this.el.length == 1) {
                this.el.attr('id', this.id);
            }
        }
    });
    app_show.getScreenScroll = function() {
        var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
        var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
        return {'scrollTop':scrollTop,'scrollLeft':scrollLeft};
    };
    app_show.getScreenSize = function() {
        var height = document.documentElement.clientHeight || document.body.clientHeight;
        var width = document.documentElement.clientWidth || document.body.clientWidth;
        return {'height':height,'width':width};
    };

    app_show.define('app_show.dialog', {
        extend:'app_show.html',
        tpl:[],
        blankClose:true,
        closeAble:true,
        show:function() {
            if (app_show.global.dialog && app_show.global.dialog !== this) {
                app_show.global.dialog.close();
            }

            app_show.global.dialog = this;

            if (this.rendered == false) {
                this.render(this.renderTo);
                this.rendered = true;
            }
            this.mask.show();
            this.el.show();

            var scroll = app_show.getScreenScroll(),
                size = app_show.getScreenSize(),
                w = this.el.width(),
                h = this.el.height();
            var top = size.height / 2 - h / 2;
            var left = size.width / 2 - w / 2;

            this.el.css({top:(top + scroll.scrollTop),left:(left + scroll.scrollLeft),position:'absolute'});
        },
        init:function() {
            var me = this;
            this.makeEl();
            if (this.closeAble) {
                this.el.find('a[title=关闭]').click(function() {
                    me.close()
                });
            } else {
                this.el.find('a[title=关闭]').parent().remove();
                this.blankClose = false;
            }

            this.mask = app_show.create('app_show.html.mask');
            $(this.mask.el[1]).click(function() {
                if (me.blankClose)
                    me.close();
            });

        },
        close:function() {
            this.el.hide();
            this.el.remove();
            app_show.global.dialog = false;
            this.mask.close();
        }
    });
    app_show.global.dialog = false;

    app_show.define('app_show.html.mask', {
        extend:'app_show.html',
        tpl:[
            '<iframe class="v-mask-iframe" frameborder="0" marginheight="0" marginwidth="0" scrolling="no" src="about:blank" style="z-index:9997;position:absolute;top:0;left:0;display:none"></iframe>',
            '<div class="v-mask" style="opacity:0.3;-moz-opacity:0.3;filter:alpha(opacity=30);position:absolute;top:0;left:0;z-index:9998;display:none"></div>'
        ],
        init:function() {
            this.makeEl();
        },
        show:function() {
            if (this.rendered == false) {
                this.render(this.renderTo);
                this.rendered = true;
            }

            var w = Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
                h = $(document).height(),
                mask = $(this.el[1]),
                mask_iframe = $(this.el[0]);
            mask_iframe.css({'width':w + 'px','height':h + 'px','display':'block','opacity':'0'});
            mask.css({'width':w + 'px','height':h + 'px','display':'block'});

            mask_iframe.show();
            mask.show();
            this.mask = mask;
            this.mask_iframe = mask_iframe;
        },
        close:function() {
            var me = this;
            this.mask.fadeOut(200, function() {
                me.mask_iframe.remove();
                me.mask.remove();
            });
        }
    });

    app_show.define('app_show.dialog.warning', {
        extend:'app_show.dialog',
        init:function(config) {
            var me = this;
            app_show.dialog.warning.superclass.init.call(this, config);
        },
        image:'styles/img/face_a.png',
        tpl:['<div style="width: {wid}px;position:relative;margin:0 auto 30px;display:none" class="dialog">' +
            '    <div class="dialog-hd"><b class="ico-ts"></b><h2 class="h">提示</h2></div>' +
            '    <div class="dialog-bd">' +
            '        <div class="act-callback">' +
            '           <img alt="" class="back-img" src="/styles/img/face_a.png"> {html}' +
            '       </div>' +
            '    </div>' +
            '    <a title="关闭" class="dialog-close" href="#">关闭</a>' +
            '</div>'
        ]
    });

    app_show.define('app_show.dialog.alert', {
        extend:'app_show.dialog',
        init:function(config) {
            var me = this;
            app_show.dialog.warning.superclass.init.call(this, config);

            if (this.autoClose) {
                setTimeout(function() {
                    me.close();
                }, 2000);
            }
        },
        image:'styles/img/face_a.png',
        tpl:['<div style="position:static;width:198px;margin:0 auto 100px;z-index:2;display:none" class="mini-alert">评论成功！</div>'
        ]
    });

    app_show.define('app_show.dialog.comment', {
        extend:'app_show.dialog',
        tpl:[
            '<div style="width:520px;position:relative;margin:0 auto 30px;display:none" class="dialog">' +
                '    <div class="dialog-bd">' +
                '    <form action="/comment.php?act=add" method="post">' +
                '        <div class="form-sya">' +
                '            <ul class="form-list">' +
                '                <li class="form-item fl"><label class="ft ftn">标题：</label><input type="text" value="30字以内" class="default input"></li>' +
                '                <li class="form-item jj">' +
                '                    <label class="ft ftn">评分：</label>' +
                '                    <div class="rate-score-sya rate-score"><ul class="rate-score-list">' +
                '                        <li class="score-item"><a class="one asc" onclick="selectStar($(this),1);return false" title="1分" href="#">1</a></li>' +
                '                        <li class="score-item"><a class="two asc" onclick="selectStar($(this),2);return false" title="2分" href="#">2</a></li>' +
                '                        <li class="score-item"><a class="three asc" onclick="selectStar($(this),3);return false" title="3分" href="#">3</a></li>' +
                '                        <li class="score-item"><a class="four asc" onclick="selectStar($(this),4);return false" title="4分" href="#">4</a></li>' +
                '                        <li class="score-item"><a class="five asc" onclick="selectStar($(this),5);return false" title="5分" href="#">5</a></li>' +
                '                   </ul><input id="rating" type="hidden" value=""><span class="p"></span></div>' +
                '                </li>' +
                '                <li class="form-item"><label class="ft ftn">评论：</label><textarea class="default input">200字以内</textarea></li>' +
                '                <li class="form-item yzm">' +
                '                    <label class="ft">验证码：</label>' +
                '                    <input type="text" value="" style="width:90px" class="input">' +
                '                    <img src="http://www.izhushou.cn/valid.php?act=valid" class="img-yzm"/>' +
                '                    <a href="javascript:void(0);" onclick="app_show.reimg($(this).prev());" class="oer">看不清，换一张</a>' +
                '                    <span class="error-ts" style="display:none;">请输入验证码</span>' +
                '                </li>' +
                '            </ul>' +
                '            <div class="form-act abso"><a onclick="return false" class="btn-syb btn" href="#"><span class="b">发表评论</span></a></div>' +
                '            <p class="tp">注意：不适当的评论将不会在爱助手中发布</p>' +
                '        </div>' +
                '    </form>' +
                '    </div>' +
                '    <a title="关闭" class="dialog-close" href="#">关闭</a>' +
                '</div>'
        ]
    });

    //发评论之前验证码
    app_show.reimg = function(o){
        o.attr({src:'http://www.izhushou.cn/valid.php?act=valid&t='+Math.random()});
    };

    app_show.ajax = {
        getArguments:function(a) {
            if (typeof a[3] == 'undefined') {
                a[3] = 'json';
            }
            if (typeof a[1] != 'object') {
                a[1] = {};
            }
            if (typeof a[1]._dc == 'undefined') {
                a[1]._dc = 1;
            }
            var d = new Date();
            a[1]._dc = Math.ceil(d.getTime() / a[1]._dc);
            return a;
        }
    };
    app_show.get = function() {
        var a = app_show.ajax.getArguments(arguments);

        function callback(s) {
            if (a[3].toLowerCase() == 'json') {
                if (typeof a[2] == 'function') {
                    a[2](s);
                }
            } else {
                if (typeof a[2] == 'function') {
                    a[2](s);
                }
            }
        }

        $.get.call(this, a[0], a[1], callback, a[3]);
    };
    app_show.post = function() {
        var a = app_show.ajax.getArguments(arguments);

        function callback(s) {
            if (a[3].toLowerCase() == 'json') {
                if (typeof a[2] == 'function') {
                    a[2](s);
                }
            } else {
                if (typeof a[2] == 'function') {
                    a[2](s);
                }
            }
        }

        $.post.call(this, a[0], a[1], callback, a[3]);
    };
})